package com.whmskj.xjlhsc.controller;

import com.whmskj.xjlhsc.controller.vo.Result;
import com.whmskj.xjlhsc.entity.*;
import com.whmskj.xjlhsc.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Version: v1.0.0
 * @Description: 机器实时数据查询接口
 * 支持查询：抓棉机、开棉机JWF1102、开棉机JWF1104、多仓混棉机、精开棉机、梳棉机、头并、末并、粗纱机、细纱机、络筒机
 **/
@RestController
@RequestMapping("/api")
public class MachineRealtimeController {

    @Autowired
    private Zmj1018RealtimeMapper zmj1018RealtimeMapper;
    
    @Autowired
    private Kmj1102RealtimeMapper kmj1102RealtimeMapper;
    
    @Autowired
    private Kmj1104aRealtimeMapper kmj1104aRealtimeMapper;
    
    @Autowired
    private Hmj1024RealtimeMapper hmj1024RealtimeMapper;
    
    @Autowired
    private Jkm1116RealtimeMapper jkm1116RealtimeMapper;
    
    @Autowired
    private SmjRealtimeMapper smjRealtimeMapper;
    
    @Autowired
    private BtjRealtimeMapper btjRealtimeMapper;
    
    @Autowired
    private CsjRealtimeMapper csjRealtimeMapper;
    
    @Autowired
    private XsjRealtimeMapper xsjRealtimeMapper;
    
    @Autowired
    private LtjRealtimeMapper ltjRealtimeMapper;

    /**
     * 根据机器类型查询实时数据
     * @param machineType 机器类型：zmj1018(抓棉机), kmj1102(开棉机JWF1102), kmj1104a(开棉机JWF1104), 
     *                    hmj1024(多仓混棉机), jkm1116(精开棉机), smj(梳棉机), btj(头并/末并), 
     *                    csj(粗纱机), xsj(细纱机), ltj(络筒机)
     * @param mesId 设备ID，可选参数
     * @param machinenumber 机台号，可选参数，如"24#"
     * @return 实时数据
     */
    @GetMapping("/machine/realtime")
    public Result getMachineRealtimeData(@RequestParam String machineType, 
                                       @RequestParam(required = false) Integer mesId,
                                       @RequestParam(required = false) String machinenumber) {
        try {
            Map<String, Object> result = new HashMap<>();
            
            switch (machineType.toLowerCase()) {
                case "zmj1018":
                    // 抓棉机
                    if (mesId != null) {
                        result.put("zmj1018", zmj1018RealtimeMapper.selectRealTimeInfo(mesId));
                    } else {
                        List<Zmj1018Realtime> zmjList = zmj1018RealtimeMapper.selectList(null);
                        result.put("zmj1018", zmjList);
                    }
                    break;
                    
                case "kmj1102":
                    // 开棉机JWF1102
                    if (mesId != null) {
                        result.put("kmj1102", kmj1102RealtimeMapper.selectRealTimeInfo(mesId));
                    } else {
                        List<Kmj1102Realtime> kmj1102List = kmj1102RealtimeMapper.selectList(null);
                        result.put("kmj1102", kmj1102List);
                    }
                    break;
                    
                case "kmj1104a":
                    // 开棉机JWF1104
                    if (mesId != null) {
                        result.put("kmj1104a", kmj1104aRealtimeMapper.selectRealTimeInfo(mesId));
                    } else {
                        List<Kmj1104aRealtime> kmj1104aList = kmj1104aRealtimeMapper.selectList(null);
                        result.put("kmj1104a", kmj1104aList);
                    }
                    break;
                    
                case "hmj1024":
                    // 多仓混棉机
                    if (mesId != null) {
                        result.put("hmj1024", hmj1024RealtimeMapper.selectRealTimeInfo(mesId));
                    } else {
                        List<Hmj1024Realtime> hmjList = hmj1024RealtimeMapper.selectList(null);
                        result.put("hmj1024", hmjList);
                    }
                    break;
                    
                case "jkm1116":
                    // 精开棉机
                    if (mesId != null) {
                        result.put("jkm1116", jkm1116RealtimeMapper.selectRealTimeInfo(mesId));
                    } else {
                        List<Jkm1116Realtime> jkmList = jkm1116RealtimeMapper.selectList(null);
                        result.put("jkm1116", jkmList);
                    }
                    break;
                    
                case "smj":
                    // 梳棉机
                    if (mesId != null) {
                        result.put("smj", smjRealtimeMapper.selectRealTimeInfo(mesId));
                    } else {
                        List<SmjRealtime> smjList = smjRealtimeMapper.selectList(null);
                        result.put("smj", smjList);
                    }
                    break;
                    
                case "btj":
                    // 头并/末并
                    if (mesId != null) {
                        result.put("btj", btjRealtimeMapper.selectRealTimeInfo(mesId));
                    } else {
                        List<BtjRealtime> btjList = btjRealtimeMapper.selectList(null);
                        result.put("btj", btjList);
                    }
                    break;
                    
                case "csj":
                    // 粗纱机
                    if (mesId != null) {
                        result.put("csj", csjRealtimeMapper.selectRealTimeInfo(mesId));
                    } else {
                        List<CsjRealtime> csjList = csjRealtimeMapper.selectList(null);
                        result.put("csj", csjList);
                    }
                    break;
                    
                case "xsj":
                    // 细纱机
                    if (mesId != null) {
                        result.put("xsj", xsjRealtimeMapper.selectRealTimeInfo(mesId));
                    } else {
                        List<XsjRealtime> xsjList = xsjRealtimeMapper.selectList(null);
                        result.put("xsj", xsjList);
                    }
                    break;
                    
                case "ltj":
                    // 络筒机
                    if (mesId != null) {
                        result.put("ltj", ltjRealtimeMapper.selectRealTimeInfo(mesId));
                    } else {
                        List<LtjRealtime> ltjList = ltjRealtimeMapper.selectList(null);
                        result.put("ltj", ltjList);
                    }
                    break;
                    
                default:
                    return Result.error("不支持的机器类型: " + machineType);
            }
            
            return Result.success(result);
            
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 查询所有机器类型的实时数据
     * @param mesId 设备ID，可选参数
     * @return 所有机器类型的实时数据
     */
    @GetMapping("/machine/realtime/all")
    public Result getAllMachineRealtimeData(@RequestParam(required = false) Integer mesId) {
        try {
            Map<String, Object> result = new HashMap<>();
            
            // 抓棉机
            if (mesId != null) {
                result.put("zmj1018", zmj1018RealtimeMapper.selectRealTimeInfo(mesId));
            } else {
                List<Zmj1018Realtime> zmjList = zmj1018RealtimeMapper.selectList(null);
                result.put("zmj1018", zmjList);
            }
            
            // 开棉机JWF1102
            if (mesId != null) {
                result.put("kmj1102", kmj1102RealtimeMapper.selectRealTimeInfo(mesId));
            } else {
                List<Kmj1102Realtime> kmj1102List = kmj1102RealtimeMapper.selectList(null);
                result.put("kmj1102", kmj1102List);
            }
            
            // 开棉机JWF1104
            if (mesId != null) {
                result.put("kmj1104a", kmj1104aRealtimeMapper.selectRealTimeInfo(mesId));
            } else {
                List<Kmj1104aRealtime> kmj1104aList = kmj1104aRealtimeMapper.selectList(null);
                result.put("kmj1104a", kmj1104aList);
            }
            
            // 多仓混棉机
            if (mesId != null) {
                result.put("hmj1024", hmj1024RealtimeMapper.selectRealTimeInfo(mesId));
            } else {
                List<Hmj1024Realtime> hmjList = hmj1024RealtimeMapper.selectList(null);
                result.put("hmj1024", hmjList);
            }
            
            // 精开棉机
            if (mesId != null) {
                result.put("jkm1116", jkm1116RealtimeMapper.selectRealTimeInfo(mesId));
            } else {
                List<Jkm1116Realtime> jkmList = jkm1116RealtimeMapper.selectList(null);
                result.put("jkm1116", jkmList);
            }
            
            // 梳棉机
            if (mesId != null) {
                result.put("smj", smjRealtimeMapper.selectRealTimeInfo(mesId));
            } else {
                List<SmjRealtime> smjList = smjRealtimeMapper.selectList(null);
                result.put("smj", smjList);
            }
            
            // 头并/末并
            if (mesId != null) {
                result.put("btj", btjRealtimeMapper.selectRealTimeInfo(mesId));
            } else {
                List<BtjRealtime> btjList = btjRealtimeMapper.selectList(null);
                result.put("btj", btjList);
            }
            
            // 粗纱机
            if (mesId != null) {
                result.put("csj", csjRealtimeMapper.selectRealTimeInfo(mesId));
            } else {
                List<CsjRealtime> csjList = csjRealtimeMapper.selectList(null);
                result.put("csj", csjList);
            }
            
            // 细纱机
            if (mesId != null) {
                result.put("xsj", xsjRealtimeMapper.selectRealTimeInfo(mesId));
            } else {
                List<XsjRealtime> xsjList = xsjRealtimeMapper.selectList(null);
                result.put("xsj", xsjList);
            }
            
            // 络筒机
            if (mesId != null) {
                result.put("ltj", ltjRealtimeMapper.selectRealTimeInfo(mesId));
            } else {
                List<LtjRealtime> ltjList = ltjRealtimeMapper.selectList(null);
                result.put("ltj", ltjList);
            }
            
            return Result.success(result);
            
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 根据机器类型列表查询多个机器类型的实时数据
     * @param machineTypes 机器类型列表，用逗号分隔
     * @param mesId 设备ID，可选参数
     * @return 指定机器类型的实时数据
     */
    @GetMapping("/machine/realtime/batch")
    public Result getBatchMachineRealtimeData(@RequestParam String machineTypes, 
                                            @RequestParam(required = false) Integer mesId) {
        try {
            Map<String, Object> result = new HashMap<>();
            String[] types = machineTypes.split(",");
            
            for (String type : types) {
                type = type.trim().toLowerCase();
                
                switch (type) {
                    case "zmj1018":
                        if (mesId != null) {
                            result.put("zmj1018", zmj1018RealtimeMapper.selectRealTimeInfo(mesId));
                        } else {
                            List<Zmj1018Realtime> zmjList = zmj1018RealtimeMapper.selectList(null);
                            result.put("zmj1018", zmjList);
                        }
                        break;
                    case "kmj1102":
                        if (mesId != null) {
                            result.put("kmj1102", kmj1102RealtimeMapper.selectRealTimeInfo(mesId));
                        } else {
                            List<Kmj1102Realtime> kmj1102List = kmj1102RealtimeMapper.selectList(null);
                            result.put("kmj1102", kmj1102List);
                        }
                        break;
                    case "kmj1104a":
                        if (mesId != null) {
                            result.put("kmj1104a", kmj1104aRealtimeMapper.selectRealTimeInfo(mesId));
                        } else {
                            List<Kmj1104aRealtime> kmj1104aList = kmj1104aRealtimeMapper.selectList(null);
                            result.put("kmj1104a", kmj1104aList);
                        }
                        break;
                    case "hmj1024":
                        if (mesId != null) {
                            result.put("hmj1024", hmj1024RealtimeMapper.selectRealTimeInfo(mesId));
                        } else {
                            List<Hmj1024Realtime> hmjList = hmj1024RealtimeMapper.selectList(null);
                            result.put("hmj1024", hmjList);
                        }
                        break;
                    case "jkm1116":
                        if (mesId != null) {
                            result.put("jkm1116", jkm1116RealtimeMapper.selectRealTimeInfo(mesId));
                        } else {
                            List<Jkm1116Realtime> jkmList = jkm1116RealtimeMapper.selectList(null);
                            result.put("jkm1116", jkmList);
                        }
                        break;
                    case "smj":
                        if (mesId != null) {
                            result.put("smj", smjRealtimeMapper.selectRealTimeInfo(mesId));
                        } else {
                            List<SmjRealtime> smjList = smjRealtimeMapper.selectList(null);
                            result.put("smj", smjList);
                        }
                        break;
                    case "btj":
                        if (mesId != null) {
                            result.put("btj", btjRealtimeMapper.selectRealTimeInfo(mesId));
                        } else {
                            List<BtjRealtime> btjList = btjRealtimeMapper.selectList(null);
                            result.put("btj", btjList);
                        }
                        break;
                    case "csj":
                        if (mesId != null) {
                            result.put("csj", csjRealtimeMapper.selectRealTimeInfo(mesId));
                        } else {
                            List<CsjRealtime> csjList = csjRealtimeMapper.selectList(null);
                            result.put("csj", csjList);
                        }
                        break;
                    case "xsj":
                        if (mesId != null) {
                            result.put("xsj", xsjRealtimeMapper.selectRealTimeInfo(mesId));
                        } else {
                            List<XsjRealtime> xsjList = xsjRealtimeMapper.selectList(null);
                            result.put("xsj", xsjList);
                        }
                        break;
                    case "ltj":
                        if (mesId != null) {
                            result.put("ltj", ltjRealtimeMapper.selectRealTimeInfo(mesId));
                        } else {
                            List<LtjRealtime> ltjList = ltjRealtimeMapper.selectList(null);
                            result.put("ltj", ltjList);
                        }
                        break;
                    default:
                        result.put("error_" + type, "不支持的机器类型: " + type);
                }
            }
            
            return Result.success(result);
            
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}
